import base64
import uuid

from django.http import JsonResponse, HttpResponse
from django.shortcuts import render, redirect


# Create your views here.
from django.urls import reverse
#
#
def  login(request):
    if request.method =='GET':
        return render(request, 'drf21_07/login.html')

    elif request.method =='POST':
        username = base64.standard_b64encode(request.POST.get('username').encode('utf-8')).decode('utf-8')
        if username:
            # 配置cookie
            response = redirect(reverse('user'))
            # 存储的cookie是明文数据
            # response.set_cookie('username',username,max_age=20)
            response.set_signed_cookie('username',username,'abcd')

            return response
        else:
            return  JsonResponse(data={
                'status':404
            })


def user(request):

    # username = request.COOKIES.get('username')
    # username = request.user.name
    # print(username)
    # username = base64.standard_b64decode(request.get_signed_cookie('username',salt='abcd').encode('utf-8')).decode('utf-8')\
    try:
        username = request.user.name
        if username:
            return HttpResponse('欢迎回家~~~~~~~~%s' %username)
        return  HttpResponse('还没有登录哦')
    except:
        return HttpResponse('还没有登录哦')

def logout(request):
    response = HttpResponse('退出登录')
    response.delete_cookie('username')

    return response

# def login(request):
#
#     if request.method =='GET':
#         return render(request, 'drf21_07/login.html')
#
#     elif request.method == 'POST':
#         username =  request.POST.get('username')
#         if username:
#             # 配置session
#             response = redirect(reverse('user'))
#             request.session['username'] = request.POST.get("username")
#             return  response
#
#
#
# def user(request):
#     username = request.session.get('username')
#     if username:
#         return HttpResponse('欢迎回家~~~~~~~~%s' %username)
#     return  HttpResponse('还没有登录哦')
#
#
# def logout(request):
#     response = HttpResponse('退出登录')
#     response.delete_cookie('sessionid')
#     del request.session['username']
#     request.session.flush()
#     return response
#
from drf21_07.models import UserToken


# def login(request):
#     if request.method == "GET":
#         return render(request, 'drf21_07/login.html')
#     elif request.method == "POST":
#         username = request.POST.get('username')
#         if UserToken.objects.filter(u_name=username).exists():
#             usertoken = UserToken.objects.filter(u_name=username).first()
#             # 设置token值
#             token = uuid.uuid4().hex
#             usertoken.u_token = token  # 保存到数据库中
#             usertoken.save()
#             return JsonResponse(
#                 data={
#                     'msg': '登录成功',
#                     'status': 201,
#                     'token': token
#                 },
#                 json_dumps_params={'ensure_ascii': False}
#             )
#         else:
#             return JsonResponse(
#                 data={
#                     'msg': '用户不存在',
#                     'status': 404
#                 }
#             )


# def user(request):
#     if request.method == "GET":
#         # 获取用户名和token
#         username = request.GET.get('username')
#         token = request.GET.get('token')
#         if UserToken.objects.filter(u_name=username).exists():
#             usertoken = UserToken.objects.filter(u_name=username).first()
#             if usertoken.u_token == token:  # 用户的token和数据库的token进行比较
#                 return JsonResponse(
#                     data={
#                         'msg': 'ok',
#                         'u_name': usertoken.u_name
#                     },json_dumps_params={'ensure_ascii':False}
#                 )
#             else:
#                 return JsonResponse(
#                     data={
#                         'msg': '无法识别用户信息',
#                         'status': 401
#                     }
#                 )
#         else:
#             return JsonResponse(
#                 data={
#                     'msg': '用户信息不存在',
#                     'status': 404
#                 }
#             )
#
#
# def logout(request):
#     username = request.GET.get('username')
#     user = UserToken.objects.filter(u_name=username).first()
#     user.u_token = ''  # 设置token为空
#     user.save()
#
#     return HttpResponse('退出登录')


